package com.laf.manager.schedule;

import com.laf.manager.dto.Member;
import com.laf.manager.dto.PointsDeduction;
import com.laf.manager.service.MemberService;
import com.laf.manager.service.PointsDeductionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ScheduledFuture;

/**
 * @author ygl
 * @create 2019-01-09
 * @DESCRIPTION
 **/
@Slf4j
@Component
public class ScheduleConfig {
    @Autowired
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;

    @Autowired
    private PointsDeductionService pointsDeductionService;

    @Autowired
    private MemberService memberService;

    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler(){
        return new ThreadPoolTaskScheduler();
    }
    private ScheduledFuture<?> future;

    private String cron = "";


    public String getCron() {
        return cron;
    }

    public void stopCron() {
        if (future != null) {
            //取消定时任务
            future.cancel(true);
        }
    }
    public void setCron(String cron) {
        this.cron = cron;
        stopCron();
        future = threadPoolTaskScheduler.schedule(new Runnable() {

            @Override
            public void run() {
                try {
                    log.info("更改停车交费状态-------");
                    /**
                     * 1.修改积分抵扣状态
                     * 2.会员积分加上去
                     */
                    List<PointsDeduction> pointsDeductionList = pointsDeductionService.findByStatus(1);
                    for (PointsDeduction pointsDeduction : pointsDeductionList) {
                        Member member = memberService.findById(pointsDeduction.getMemberId());
                        if (member != null) {
                            // 累加用户可用积分
                            int usable_points = member.getUsable_points();
                            int deductionPoints = pointsDeduction.getDeductionPoints();
                            member.setUsable_points(usable_points+deductionPoints);
                            memberService.updatePoints(member.getMember_id(),member.getCumulate_points(),member.getUsable_points());
                            // 修改状态为已撤销
                            pointsDeduction.setStatus(3);
                            pointsDeduction.setBizDate(new Date());
                            pointsDeductionService.updateStatus(pointsDeduction);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                if ("".equals(cron) || cron == null) {
                    return null;
                }
                // 定时任务触发，可修改定时任务的执行周期
                CronTrigger trigger = new CronTrigger(cron);
                Date nextExecDate = trigger.nextExecutionTime(triggerContext);
                return nextExecDate;
            }
        });
    }
}
